package com.spark.trouble.agency

import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.google.gson.Gson
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.helper.MutableStatePagerAdapter
import com.spark.common.model.AgencyTodoListModel
import com.spark.trouble.*
import com.spark.trouble.handle.LimitHandleFragment
import kotlinx.android.synthetic.main.act_handle.*

/**
 * [待办] 隐患处理页
 * [任务类型]
 * 1 -> 标准化检查 -> 主任务
 * 2 -> 标准化检查 -> 子任务
 * 3 -> 借阅申请
 * 4 -> 文件传输
 * 5 -> 归档申请
 * 6 -> 隐患 -> 主任务
 * 7 -> 隐患 -> 子任务(未分配)
 * 8 ->     -> 现场治理
 * 9 ->     -> 限时整改
 * 10 ->    -> 挂牌督办
 * 11 ->    -> 隐患升级
 * 12 ->    -> 重大隐患
 */
class HTAgencyFragment : BaseFragment() {
    private lateinit var htAgencyAdapter: MutableStatePagerAdapter

    companion object {
        fun newInstance(data: String): HTAgencyFragment {
            val target = HTAgencyFragment()
            val args = Bundle()
            args.putString("data", data)
            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.act_handle
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { activity?.finish() }
        arguments?.apply {
            getString("data")?.apply {
                val prop = this
                Gson().fromJson(this, AgencyTodoListModel.Data.Page.Result::class.java)?.apply {
                    val tabTitles = mutableListOf<String>()
                    val fragments = mutableListOf<Fragment>()
                    when (taskTypeFlag) {
                    // 未分配任务
                        7 -> {
                        }
                    // 现场治理
                        8 -> {
                            tv_title.text = "待审核"
                            // 仅有【待审核】状态
                            tabTitles.apply {
                                add("现场情况")
                                add("处罚方案")
                                add("治理方案")
                            }
                            fragments.apply {
                                add(HTInfoFragment.newInstance(Const.htTypeAgency, id.toString(), "$taskTypeFlag"))
                                add(PunishSchemaFragment.newInstance(id.toString()))
                                add(GovernSchemeFragment.newInstance(id.toString()))
                            }
                        }
                    // 限时整改
                    // 挂牌督办
                        9, 10 -> {
                            when (states) {
                            // 待审核
                            // 待确认
                                "2", "4" -> {
                                    tv_title.text = if (states == "2") "待审核" else "待确认"
                                    tabTitles.apply {
                                        add("现场情况")
                                        add("整改通知书")
                                    }
                                    fragments.apply {
                                        add(HTInfoFragment.newInstance(Const.htTypeAgency, id.toString(), "$taskTypeFlag"))
                                        add(LimitHandleFragment.newInstance(Const.htTypeAgency, prop, "$taskTypeFlag"))
                                    }
                                }
                            // 待整改
                                "5" -> {
                                    tv_title.text = "待整改"
                                    tabTitles.apply {
                                        add("整改通知书")
                                        add("现场情况")
                                    }
                                    fragments.apply {
                                        add(LimitHandleFragment.newInstance(Const.htTypeAgency, prop, "$taskTypeFlag"))
                                        add(HTInfoFragment.newInstance(Const.htTypeAgency, id.toString(), "$taskTypeFlag"))
                                    }
                                }
                            }
                        }
                    // 隐患升级
                        11 -> {
                            // 仅有【待审】核状态
                            tv_title.text = "待审核"
                            tabTitles.apply {
                                add("现场情况")
                                add("隐患升级")
                            }
                            fragments.apply {
                                add(HTInfoFragment.newInstance(Const.htTypeAgency, id.toString(), "$taskTypeFlag"))
                                add(HTUpgradeFragment.newInstance(id.toString()))
                            }
                        }
                    // 重大隐患[重大治理任务]
                        12 -> {
                            // 仅有【待审核】状态
                            tv_title.text = statesstr
                            tabTitles.apply {
                                add("方案简述")
                                add("现场情况")
                                add("责任内容")
                                add("治理方案")
                                add("预案措施")
                                add("资金方案")
                            }
                            fragments.apply {
                                add(SchemaBriefFragment.newInstance(id.toString()))
                                add(HTInfoFragment.newInstance(Const.htTypeAgency, id.toString(), "$taskTypeFlag"))
                                add(ResponsbleContentFragment.newInstance(id.toString()))
                                add(HandleSchemaFragment.newInstance(id.toString()))
                                add(PrePlanMeasureFragment.newInstance(id.toString()))
                                add(FundingPlanFragment.newInstance(id.toString()))
                            }
                            tabs.tabMode = TabLayout.MODE_SCROLLABLE
                        }
                    /**
                     * 验收驳回 -> 已驳回
                     */
                    }
                    // [end of when]
                    tabTitles.forEach {
                        tabs.addTab(tabs.newTab().setText(it))
                    }
                    htAgencyAdapter = object : MutableStatePagerAdapter(childFragmentManager) {
                        override fun getCount(): Int {
                            return tabTitles.size
                        }

                        override fun getPageTitle(position: Int): CharSequence? {
                            return tabTitles[position]
                        }
                    }
                    htAgencyAdapter.adds(fragments)
                    pager.apply {
                        adapter = htAgencyAdapter
                        canScroll = true
                    }
                    tabs.setupWithViewPager(pager)
                    LogUtils.e("$tabTitles【taskType】$taskTypeFlag -> $tasktype 【task_id】$id")
                }
            }
        }
    }
}